package com.xuan.dao;
import com.xuan.model.Acc_details;
import com.xuan.model.Game;
import com.xuan.model.Section;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class AccDetailsDao extends BaseDao{
    public boolean add (Acc_details acc_details) {
        String sql = "insert into accompanying_play_detail" +
                "  (detail_id, top_img, state, serve_count, " +
                "order_receiving_time, can_yield, can_dan, " +
                "game_feature)" +
                " values" +
                "  (?,?,?,?,?,?,?,?)";
        try {
            return queryRunner.execute(sql,acc_details.getDetail_id(),
                    acc_details.getTop_img(),
                    acc_details.getState(),
                    acc_details.getServe_count(),
                    acc_details.getOrder_receiving_time(),
                    acc_details.getCan_yield(),
                    acc_details.getCan_dan(),
                    acc_details.getGame_feature()
                    ) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public boolean delete (int id) {
        String sql = "delete from accompanying_play_detail where detail_id = ?";
        try {
            return queryRunner.execute(sql,id) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public boolean update (Acc_details acc_details) {
        String sql = "update accompanying_play_detail" +
                " set " +
                " top_img = ?," +
                " state = ?," +
                " serve_count = ?," +
                " order_receiving_time = ?," +
                " can_yield = ?," +
                " can_dan = ?," +
                " game_feature = ?," +
                " accompanying_play_id = ?" +
                " where detail_id = ?";
        Acc_details acc_details1 = get(acc_details.getDetail_id());
        if (acc_details.getTop_img() == null && acc_details1 != null) {
            acc_details.setTop_img(acc_details1.getTop_img());
        }
        try {
            return queryRunner.execute(sql,
                    acc_details.getTop_img(),
                    acc_details.getState(),
                    acc_details.getServe_count(),
                    acc_details.getOrder_receiving_time(),
                    acc_details.getCan_yield(),
                    acc_details.getCan_dan(),
                    acc_details.getGame_feature(),
                    acc_details.getAccompanying_play().getGame_id(),
                    acc_details.getDetail_id()
                    ) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public List<Acc_details> getAll (int size,int page) {
        List<Acc_details> list = new LinkedList<>();
        String sql="select ap.id apid, ap.name apname, level_, order_count,"
                + "loc, acc_img, message, price,"
                + "dan,ge.id geid, ge.name gename, icon,"
                + "detail_id, top_img, state,"
                + "serve_count, order_receiving_time,"
                + "can_yield, can_dan, game_feature"
                + " from accompanying_play ap"
                + " join game ge on ap.game_id = ge.id"
                + " join accompanying_play_detail apd on apd.accompanying_play_id = ap.id" +
                " where detail_id not in (select detail_id from accompanying_play_detail where rownum <= ?) and rownum <= ?" +
                " order by apid";
        try {
            return queryRunner.query(sql, new ResultSetHandler<List<Acc_details>>() {
                @Override
                public List<Acc_details> handle(ResultSet rs) throws SQLException {
                    while (rs.next()) {
                        list.add(new Acc_details(
                                rs.getInt("detail_id"),
                                rs.getString("top_img"),
                                rs.getInt("state"),
                                rs.getInt("serve_count"),
                                rs.getString("order_receiving_time"),
                                rs.getString("can_yield"),
                                rs.getString("can_dan"),
                                rs.getString("game_feature"),
                                new Section(
                                        rs.getInt("apid"),
                                        rs.getString("apname"),
                                        rs.getDouble("level_"),
                                        rs.getInt("order_count"),
                                        rs.getString("loc"),
                                        rs.getString("acc_img"),
                                        rs.getString("message"),
                                        rs.getInt("price"),
                                        rs.getString("dan"),
                                        new Game(
                                                rs.getInt("geid"),
                                                rs.getString("gename")
                                        )
                                )
                        ));
                    }
                    return list;
                }
            },(page-1)*size,size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public Acc_details get(int id) {
        try {
            String sql="select ap.id apid, ap.name apname, level_, order_count,"
                    + "loc, acc_img, message, price,"
                    + "dan,ge.id geid, ge.name gename, icon,"
                    + "detail_id, top_img, state,"
                    + "serve_count, order_receiving_time,"
                    + "can_yield, can_dan, game_feature"
                    + " from accompanying_play ap"
                    + " join game ge on ap.game_id = ge.id"
                    + " join accompanying_play_detail apd on apd.accompanying_play_id = ap.id" +
                    " and ap.id = ?";
            return queryRunner.query(sql, new ResultSetHandler<Acc_details>() {
                @Override
                public Acc_details handle(ResultSet rs) throws SQLException {
                    if(rs.next()) {
                        return new Acc_details(
                                rs.getInt("detail_id"),
                                rs.getString("top_img"),
                                rs.getInt("state"),
                                rs.getInt("serve_count"),
                                rs.getString("order_receiving_time"),
                                rs.getString("can_yield"),
                                rs.getString("can_dan"),
                                rs.getString("game_feature"),
                                new Section(
                                        rs.getInt("apid"),
                                        rs.getString("apname"),
                                        rs.getDouble("level_"),
                                        rs.getInt("order_count"),
                                        rs.getString("loc"),
                                        rs.getString("acc_img"),
                                        rs.getString("message"),
                                        rs.getInt("price"),
                                        rs.getString("dan"),
                                        new Game(
                                                rs.getInt("geid"),
                                                rs.getString("gename")
                                        )
                                )
                        );
                    }
                    return null;
                }
            },id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public long getPageSize(int size) {
        String sql = "select count(1) from accompanying_play_detail";
        try {
            double res = Double.valueOf(queryRunner.query(sql,new ScalarHandler<>()).toString());
            return (long)Math.ceil(res/size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
